/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.googlecode.ivybeans.module.nodes;

import com.googlecode.ivybeans.facade.ArtifactInfo;
import com.googlecode.ivybeans.facade.ResolvedIvyLibrary;
import com.googlecode.ivybeans.module.IvyLibraryController;
import com.googlecode.ivybeans.module.classpath.ClassPathScope;
import com.googlecode.ivybeans.module.resources.ResourcesAccess;
import java.awt.Image;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Collection;
import org.netbeans.api.project.Project;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.ImageUtilities;
import org.openide.util.NbBundle;

/**
 *
 * @author lforet
 */
public class ScopeNode extends AbstractFolderNode {

    private static final Image ICON_BADGE = ImageUtilities.loadImage("org/netbeans/modules/java/j2seproject/ui/resources/libraries-badge.png");    //NOI18N
    private ClassPathScope scope;
    
    public ScopeNode(Project project, ClassPathScope scope) {
        super(new ScopesChildren(project, scope));
        this.scope = scope;
    }

    @Override
    public String getDisplayName() {
        return NbBundle.getMessage(ResourcesAccess.class, "CTL_Scope_"+scope.name());
    }

    @Override
    public String getName() {
        return scope.name();
    }

    @Override
    protected Image getBadge() {
        return ICON_BADGE;
    }

    
    private static class ScopesChildren extends Children.Keys<ArtifactInfo> implements PropertyChangeListener {

        private Project project;
        private ClassPathScope scope;
        
        public ScopesChildren(Project project, ClassPathScope scope) {
            this.project = project;
            this.scope = scope;
            setKeys();
            IvyLibraryController ivyLibraryController = project.getLookup().lookup(IvyLibraryController.class);
            ivyLibraryController.addPropertyChangeListener(this);
        }

        @Override
        protected Node[] createNodes(ArtifactInfo key) {
            return new Node[]{new ArtifactNode(key, project)};
        }

        public void propertyChange(PropertyChangeEvent evt) {
            if (IvyLibraryController.IVY_LIBRARY_RESOLVED.equals(evt.getPropertyName())) {
                setKeys();
            }
        }

        private void setKeys() {
            IvyLibraryController ivyLibraryController = project.getLookup().lookup(IvyLibraryController.class);
            ResolvedIvyLibrary resolvedIvyLibrary = ivyLibraryController.getResolvedLibrary(scope);
            if (resolvedIvyLibrary != null) {
                Collection<ArtifactInfo> artifacts = resolvedIvyLibrary.getArtifacts();
                setKeys(artifacts);
            }
        }
    }

    
}
